package com.buss.product.service.impl;

import java.util.ArrayList;
import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.buss.core.dic.RecordStatus;
import com.buss.core.exception.ServiceException;
import com.buss.core.persistence.OrderBy;
import com.buss.core.persistence.Paginalbe;
import com.buss.core.persistence.ResultList;
import com.buss.product.dao.GoodsCategoryDao;
import com.buss.product.model.GoodsCategory;
import com.buss.product.service.GoodsCategoryService;

@Service
public class GoodsCategoryServiceImpl implements GoodsCategoryService {

	@Autowired
	private GoodsCategoryDao categoryDao;
	
	@Override
	public GoodsCategory saveCategory(GoodsCategory category) throws ServiceException {
		if(categoryDao.loadCategory(category.getCompanyId(), category.getCode()) != null){
			throw new ServiceException("分类编码已经存在，请核实!");
		}
		category.setSubCategories(0);
		return categoryDao.saveCategory(category);
	}

	@Override
	public void editCategory(GoodsCategory category) {
		categoryDao.editCategory(category);
	}

	public void updateCategoryBrand(Long categoryId,Long[] brandIds){
		categoryDao.updateCategoryBrand(categoryId, brandIds);
	}
	
	public void updateCategoryPropGroup(Long categoryId,Long[] groupIds){
		categoryDao.updateCategoryPropGroup(categoryId, groupIds);
	}
	
	@Override
	public void removeCategory(Long[] ids) {
		categoryDao.removeCategory(ids);
	}

	@Override
	public GoodsCategory loadCategory(Long id) {		
		return categoryDao.loadCategory(id);
	}

	@Override
	public GoodsCategory loadCategory(String companyId, String code) {
		return categoryDao.loadCategory(companyId, code);
	}

	@Override
	public ResultList<GoodsCategory> queryCategory(String companyId,
			String branchId, Long parentId, String name, RecordStatus status,
			Long propGroupId, OrderBy orderBy, Paginalbe page) {
		return categoryDao.queryCategory(companyId, branchId, parentId, name, status, propGroupId, orderBy, page);
	}
	
	public List<GoodsCategory> queryCategoryByParentId(String companyId,String branchId,Long parentId){
		List<GoodsCategory> list = queryCategory(companyId, branchId, parentId, null, RecordStatus.normal, null, null, null).getData();
		List<GoodsCategory> tmpList = new ArrayList<GoodsCategory>();
		for(GoodsCategory category : list){
			tmpList.add(category);
			getChildCategory(companyId,branchId,category.getId(), tmpList);
		}
		return tmpList;
	}
	
	private void getChildCategory(String companyId,String branchId,Long parentId,List<GoodsCategory> tmpList){
		List<GoodsCategory> list = queryCategory(companyId, branchId, parentId, null, RecordStatus.normal, null, null, null).getData();
		for(GoodsCategory category : list){
			tmpList.add(category);
			getChildCategory(companyId,branchId,category.getId(), tmpList);
		}
	}
	
	public List<GoodsCategory> queryCategoryByChildId(Long categoryId){
		List<GoodsCategory> list = new ArrayList<GoodsCategory>();
		GoodsCategory category = loadCategory(categoryId);
		list.add(category);
		if(category != null && category.getParentId() != 0){
			getParentCategory(category.getParentId(),list);
		}
		return list;
	}
	
	private void getParentCategory(Long categoryId,List<GoodsCategory> list){
		GoodsCategory category = loadCategory(categoryId);
		if(category != null){
			list.add(category);
			getParentCategory(category.getParentId(), list);
		}
	}
}
